var configTitles = ["Language", "Credits", "Resolution" ];
var configOnSelections = ["Japanese", "On", "640×480" ];
var configOffSelections = ["English", "Off", "854×480" ];// ◄16:9▶
var configSettings = [ System.isLanguagePrimary(), getFlag("Credit"), [Renderer.getWidth(), Renderer.getHeight()] ];
var cursorX = 0;
var cursorY = 0;
//function ConfigInit()
//{
//	cursorX = 0;
//	cursorY = 0;
//}

function ConfigIsOn(index)
{
	if (index == 0) return System.isLanguagePrimary();
	if (index == 1) return !getFlag("Credit");
	if (index == 2) return Renderer.getWidth() == 640 && Renderer.getHeight() == 480;
	return false;
}

function ConfigSwitch(index)
{
	if (index == 0) {
		System.toggleLanguages();
	}
	if (index == 1) {
		setFlag("Credit", ConfigIsOn(index));
	}
	if (index == 2) {
		if (ConfigIsOn(index))
			GameSettings.setResolution(854, 480);
		else
			GameSettings.setResolution(640, 480);
	}
}

function ConfigOK()
{
	//sceneFunction = TitleScene;
	save();
	changeScene("Title");
}

function ConfigCancel()
{
	if (configSettings[0] != System.isLanguagePrimary())
		System.toggleLanguages();
	setFlag("Credit", configSettings[1]);
	GameSettings.setResolution(configSettings[2][0], configSettings[2][1]);
	changeScene("Title");
}

// Game.txt の onEnterFrame() で毎フレームごとにする処理
function ConfigScene(g: Graphics)
{
	ConfigUpdate();
	ConfigDraw(g);
}

function ConfigUpdate()
{
	if (Key.isTyped(Key.RIGHT)) {
		cursorX++;
	}
	else if (Key.isTyped(Key.LEFT)) {
		cursorX--;
	}
	else if (Key.isTyped(Key.UP)) {
		cursorY--;
		if (cursorY < 0)
			cursorY = configTitles.length();
	}
	else if (Key.isTyped(Key.DOWN)) {
		cursorY++;
		if (cursorY > configTitles.length())
			cursorY = 0;
	}
	else if (Key.isCancelKeyTyped()) {
		ConfigCancel();
	}
	else if (Key.isTyped(Key.START)) {
		ConfigOK();
	}
	else if (Key.isOkKeyTyped()) {
		ConfigSwitch(cursorY);
		if (cursorY == configTitles.length()) {
			ConfigOK();
		}
	}
}

function ConfigDraw(g: Graphics)
{
	// GameSettings.setResolution() でサイズが変更された場合はリサイズ
	if (g.getWidth() != Renderer.getWidth() || g.getHeight() != Renderer.getHeight()) {
		g.setSize(Renderer.getWidth(), Renderer.getHeight());
	}
	// フォントサイズを解像度に応じて変更
	FONT_SIZE = Graphics.getDefaultFontSize();

	// draw frame
	var x = FONT_SIZE;
	var y = FONT_SIZE;
	g.drawFrame(x, y, Renderer.getWidth()-x*2, Renderer.getHeight()-y*2);

	// draw config items
	//var width = FONT_SIZE*6;
	//var width2 = FONT_SIZE*4;;
	var width  = FONT_SIZE*10;
	var width2 = FONT_SIZE*7;
	var height = FONT_SIZE*1.6;
	x = (Renderer.getWidth()-width-width2*2)/2;
	y = FONT_SIZE*8;//FONT_SIZE*2;
	g.setFontSize(FONT_SIZE*1.4);
//	g.setTextAlign(TextAlign.LEFT); // テキストを左寄せに設定
g.setTextAlign(TextAlign.CENTER); // テキストを中央寄せに設定
	for (var i = 0; i < configTitles.length; ++i) {
		var focused = cursorY == i;
		ConfigDrawItem(g, configTitles[i],        x,              y, width, height, true, focused);
		ConfigDrawItem(g, configOnSelections[i] , x+width,        y, width2, height, ConfigIsOn(i), focused);
		ConfigDrawItem(g, configOffSelections[i], x+width+width2, y, width2, height, !ConfigIsOn(i), focused);
		y += height;
	}
	ConfigDrawItem(g, "Return to Title", x, y, width+width2*2, height, true, cursorY == configTitles.length);

	// draw help
//	var titleText = 
//		"[A]で　へんこう　[B]で　キャンセル\n"
//		"\n"
//		"[START]で　しゅうりょう           ";
	var titleText = 
		"Press [A] to change. Press [B] to cancel\n"
		"\n"
		"Press [START] to exit                   ";
	g.setFontSize(FONT_SIZE*0.8);
	g.drawString(titleText, Renderer.getWidth()/2, Renderer.getHeight()-FONT_SIZE*6);
}

function ConfigDrawItem(g: Graphics, text: String, x, y, width, height, selected, focused)
{
	// draw background
	if (focused) {
		g.setFillColor(#f00);
	}
	else {
		g.setFillColor(#2d2d2d);
	}
	g.fillRect(x, y, width, height);
	text = StringTable.get(text);
	// draw text
	if (selected) {
		g.drawBorderString(text.toUpperCase(), x+width/2, y+2, #ff3, 0);
	}
	else {
		g.drawBorderString(text.toUpperCase(), x+width/2, y+2, #111, 0);
	}
	
	// draw frame
	g.drawFrame(x, y, width, height);
}

//	if (index == 2) {
//		if (ConfigIsOn(index))
//			GameSettings.setAspectRatio(16 / 9);
//		else
//			GameSettings.setAspectRatio(4 / 3);
//	}
//	var titleText = 
//		"[A]で　へんこう                   \n"
//		"\n"
//		"[B]か[START]で　しゅうりょう      ";
//var soundEnabled: String = IniFile.read("Config", "soundEnabled");
//var language: String = IniFile.read("Config", "language");
//			IniFile.write("Config", "soundEnabled", soundEnabled);
//			Audio.setSoundEnabled(soundEnabled);